package com.chinasoft.goodsManager.servlet;

import com.chinasoft.goodsManager.doamain.Order;
import com.chinasoft.goodsManager.service.OrderService;
import com.chinasoft.goodsManager.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Set;

@WebServlet("/orderUpdate")
public class OrderUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<String> strings = parameterMap.keySet();
        for (String string : strings) {
            System.out.println(string + parameterMap.get(string)[0]);
        }
        System.out.println(req.getParameter("single-date-picker"));
        Order order = new Order();
        order.setId(Integer.parseInt(req.getParameter("uid")));
        order.setPname(req.getParameter("upname"));
        try {
            order.setOdate(new SimpleDateFormat("MM/dd/yyyy").parse(req.getParameter("single-date-picker")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setAddress(req.getParameter("uaddress"));
        order.setCategory(req.getParameter("uleibie"));
        order.setPrice(Double.parseDouble(req.getParameter("utprice")));
        order.setNum(Integer.parseInt(req.getParameter("unum")));
        order.setOprice(Double.parseDouble(req.getParameter("uoprice")));
        OrderService service = new OrderServiceImpl();
        System.out.println(order);
        boolean flag = service.update(order);
        System.out.println(flag);
        resp.setHeader("Content-Type", "application/json;charset=UTF-8");
        if (flag) {
            resp.getWriter().write("1");
        } else
            resp.getWriter().write("0");
    }

}
